package com.houger.ddd.seckill.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.houger.ddd.common.utils.R;
import com.houger.ddd.seckill.facade.SeckillFacede;
import com.wf.captcha.SpecCaptcha;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author Houger.
 * @date 2024年05月15日 10:39.
 */
@RefreshScope
@Slf4j
@Tag(name = "seckill")
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    public static final String SECKILL_URI="/doseckill/%s";
    @Resource
    SeckillFacede seckillFacede;


    @PostMapping("/trySeckill/{templateId}")
    @Operation(summary = "trySeckill", description = "获取秒杀链接")
    public R<String> trySeckill(@PathVariable Long templateId, @RequestHeader Long userId) {
        log.info("userId={}",userId);
        return seckillFacede.getSeckillUri(templateId,userId);
    }

    @PostMapping("/captcha/{instanceId}/{userId}")
    @Operation(summary = "captcha", description = "获取验证码")
    public R<String> captcha(@PathVariable Long instanceId, @PathVariable Long userId, HttpServletResponse response){
        // 设置请求头为输出图片类型
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        return seckillFacede.captcha(instanceId,userId);

    }

    @PostMapping("/doseckill/{instanceId}/{userId}/{itemId}/{captcha}")
    @Operation(summary = "doseckill", description = "发起秒杀")
    public R<String> doseckill(@PathVariable Long instanceId, @PathVariable Long userId,@PathVariable Long itemId,@PathVariable String captcha){
        log.info("instanceId={},userId={},captcha={}",instanceId,userId,captcha);
        return seckillFacede.doSeckill(instanceId,userId,itemId,captcha);
    }



}
